package com.umeox.watch.moto.launcher.fragment;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.BaseApplication;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.bean.AppGroupInfo;
import com.umeox.watch.moto.launcher.bean.AppInfo;
import com.umeox.watch.moto.launcher.constant.AppEntry;
import com.umeox.watch.moto.launcher.event.HorizontalScrollToPagerEvent;
import com.umeox.watch.moto.launcher.ui.DialPadActivity1;
import com.umeox.watch.moto.launcher.ui.GuideActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class AppListConfigureInfo {

    public static AppListConfigureInfo INSTANCE = new AppListConfigureInfo();

    private AppListConfigureInfo() {

    }

    private AppInfo appPhone;
    private AppInfo appContacts;
    private AppInfo appChat;
    private AppInfo appInfo6;
    private AppInfo appInfo7;
    private AppInfo appChallenge;
    private AppInfo appInfo9;
    private AppInfo appInfo10;
    private AppInfo appInfo11;

    private AppInfo appInfo12;
    private AppInfo appInfo13;
    private AppInfo appInfo14;
    private AppInfo appInfo15;
    private AppInfo appInfo16;
    private AppInfo appPrayerTime;

    private AppInfo appAlphabetLearn;

    private AppInfo stories;

    private AppInfo gallery;
    private AppInfo stopwatch;
    private AppInfo timer;
    private AppInfo calendar;
    private AppInfo calculator;
    private AppInfo recorder;
    private AppInfo arithmeticGame;
    private AppInfo tetris;
    private AppInfo appTasbih;
    private AppInfo appSMS;
    private AppInfo customTasbih;
    private AppInfo qimi;

    //添加的测试数据采集APP
    private AppInfo appMotionCapture;
    private AppInfo whatsApp;

    public void initAppInfo() {
        appPhone = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_phone), R.drawable.app_icon_phone, AppEntry.ContactsMain.PKG, AppEntry.ContactsMain.CLS);
        appContacts = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_contact), R.drawable.app_icon_contact, AppEntry.Contacts.PKG, AppEntry.Contacts.CLS);
        appChat = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_friends), R.drawable.app_icon_friend, AppEntry.Chat.PKG, AppEntry.Chat.CLS);
        appInfo6 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_camera), R.drawable.app_icon_camera, AppEntry.Camera.PKG, AppEntry.Camera.CLS);
        appInfo7 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_alarm), R.drawable.app_icon_alarm_small, AppEntry.Alarm.PKG, AppEntry.Alarm.CLS);
        appChallenge = new AppInfo(R.string.menu_activity, R.drawable.app_icon_activity, AppEntry.Pedometer.PKG, AppEntry.Pedometer.CLS);
        appInfo9 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_setting), R.drawable.app_icon_settings, AppEntry.Settings.PKG, AppEntry.Settings.CLS);
        appInfo10 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_music), R.drawable.app_icon_music, AppEntry.MediaPlayer.PKG, AppEntry.MediaPlayer.CLS);
        appInfo11 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_guide), R.drawable.app_icon_guide, AppEntry.Guide.PKG, AppEntry.Guide.CLS);

        appInfo12 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_pary), R.drawable.app_icon_pary_small, AppEntry.Pary.PKG, AppEntry.Pary.CLS);
        appInfo13 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_wudu), R.drawable.app_icon_wudu_small, AppEntry.Wudu.PKG, AppEntry.Wudu.CLS);
        appInfo14 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_lslam), R.drawable.app_icon_lslam_small, AppEntry.Lslam.PKG, AppEntry.Lslam.CLS);
        appInfo15 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_lman), R.drawable.app_icon_lman_small, AppEntry.Lman.PKG, AppEntry.Lman.CLS);
        appInfo16 = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_quran_player), R.drawable.app_icon_quran_player_small, AppEntry.QuranPlayer.PKG, AppEntry.QuranPlayer.CLS);
        appPrayerTime = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_prayer_time), R.drawable.app_icon_prayer_time, AppEntry.PrayerTime.PKG, AppEntry.PrayerTime.CLS);

        appAlphabetLearn = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_alphabet_learn), R.drawable.app_icon_alphabet_learn_small, AppEntry.AlphabetLearn.PKG, AppEntry.AlphabetLearn.CLS);

        stories = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_stories), R.drawable.app_icon_story_small, AppEntry.Stories.PKG, AppEntry.Stories.CLS);

        gallery = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_photos), R.drawable.app_icon_gallery, AppEntry.Gallery.PKG, AppEntry.Gallery.CLS);
        stopwatch = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_stopwatch), R.drawable.app_icon_stopwatch_small, AppEntry.Stopwatch.PKG, AppEntry.Stopwatch.CLS);
        timer = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_timer), R.drawable.app_icon_timer_small, AppEntry.Timer.PKG, AppEntry.Timer.CLS);
        calendar = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_calendar), R.drawable.app_icon_calendar_small, AppEntry.Calendar.PKG, AppEntry.Calendar.CLS);
        calculator = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_calculator), R.drawable.app_icon_calculator_small, AppEntry.Calculator.PKG, AppEntry.Calculator.CLS);
        recorder = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_recorder), R.drawable.app_icon_recorder_small, AppEntry.Recorder.PKG, AppEntry.Recorder.CLS);
        arithmeticGame = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_game), R.drawable.app_icon_arithmetic_small, AppEntry.Arithmetic.PKG, AppEntry.Arithmetic.CLS);
        tetris = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_tetris), R.drawable.app_icon_tetris_small, AppEntry.GameTetris.PKG, AppEntry.GameTetris.CLS);
        appTasbih = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_tashbih), R.drawable.app_icon_tasbih, AppEntry.Tasbih.PKG, AppEntry.Tasbih.CLS);
        appSMS = new AppInfo("SMS", R.drawable.app_icon_sms, AppEntry.SMS.PKG, AppEntry.SMS.CLS);
        customTasbih = new AppInfo(BaseApplication.getInstance().getString(R.string.menu_custom_tasbih), R.drawable.app_icon_custom_tasbih, AppEntry.CustomTasbih.PKG, AppEntry.CustomTasbih.CLS);
        qimi = new AppInfo("Qimi", R.drawable.app_icon_qimi, AppEntry.Qimi.PKG, AppEntry.Qimi.CLS);

        //添加的测试数据采集APP
        appMotionCapture = new AppInfo("Motion Capture", R.drawable.app_icon_motion_capture, AppEntry.MotionCapture.PKG, AppEntry.MotionCapture.CLS);
        whatsApp = new AppInfo("WhatsApp", R.drawable.app_icon_whatsapp, AppEntry.WhatsApp.PKG, AppEntry.WhatsApp.CLS);
        bindStatusAppGroupList = null;
        unBindStatusAppGroupList = null;
    }


    private ArrayList<AppGroupInfo> bindStatusAppGroupList;
    private ArrayList<AppGroupInfo> unBindStatusAppGroupList;

    public ArrayList<AppGroupInfo> getAppGroupList(boolean isBind) {
        if (isBind) {
            if (bindStatusAppGroupList == null) {
                bindStatusAppGroupList = getBindStatusAppGroupList();
            }
            return bindStatusAppGroupList;
        } else {
            if (unBindStatusAppGroupList == null) {
                unBindStatusAppGroupList = getUnbindStatusAppGroupList();
            }
            return unBindStatusAppGroupList;
        }
    }

    public void updateBindStatusAppGroupList() {
        if (bindStatusAppGroupList == null) {
            return;
        }

        ArrayList<AppGroupInfo> tempList = getBindStatusAppGroupList();
        bindStatusAppGroupList.clear();
        bindStatusAppGroupList.addAll(tempList);
    }

    private ArrayList<AppGroupInfo> getBindStatusAppGroupList() {
        ArrayList<AppGroupInfo> tempList = new ArrayList<>();
        //Phone
        AppGroupInfo phoneGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_phone), R.drawable.menu_icon_phone);
        phoneGroup.addAppInfo(appPhone);
        tempList.add(phoneGroup);
        //WhatsApp
        AppGroupInfo whatsAppGroup = new AppGroupInfo("WhatsApp", R.drawable.menu_icon_whatsapp);
        whatsAppGroup.addAppInfo(whatsApp);
        tempList.add(whatsAppGroup);
        //Message
        AppGroupInfo messageGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_friends), R.drawable.menu_icon_message);
        messageGroup.addAppInfo(appChat);
        tempList.add(messageGroup);
        //Prayer Time
        AppGroupInfo prayerTimeGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_prayer_time), R.drawable.menu_icon_prayer);
        prayerTimeGroup.addAppInfo(appPrayerTime);
        tempList.add(prayerTimeGroup);
        //Qimi
        if (DataProvider.getQimi()) {
            AppGroupInfo qimiGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.qimi_ai), R.drawable.app_icon_qimi);
            qimiGroup.addAppInfo(qimi);
            tempList.add(qimiGroup);
        }

        //Tasbih
        AppGroupInfo tasbihGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_tashbih), R.drawable.menu_icon_tasbih);
        tasbihGroup.addAppInfo(appTasbih);
        tempList.add(tasbihGroup);
        //customTasbih
        AppGroupInfo customTasbihGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_custom_tasbih), R.drawable.app_icon_custom_tasbih);
        customTasbihGroup.addAppInfo(customTasbih);
        tempList.add(customTasbihGroup);
        //Quran Player
        AppGroupInfo quranPlayer = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_quran_player), R.drawable.app_icon_quran_player);
        quranPlayer.addAppInfo(appInfo16);
        tempList.add(quranPlayer);
        //Religious Education
        AppGroupInfo religiousEducationGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_education), R.drawable.menu_icon_education);
        religiousEducationGroup.addAppInfo(appInfo13);
        religiousEducationGroup.addAppInfo(appInfo12);
        religiousEducationGroup.addAppInfo(appInfo14);
        religiousEducationGroup.addAppInfo(appInfo15);
        religiousEducationGroup.addAppInfo(appAlphabetLearn);
        tempList.add(religiousEducationGroup);
        //Challenge
        AppGroupInfo challengeGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_activity), R.drawable.menu_icon_challenge);
        challengeGroup.addAppInfo(appChallenge);
        tempList.add(challengeGroup);
        //Games
        AppGroupInfo gamesGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_games), R.drawable.menu_icon_games);
        gamesGroup.addAppInfo(stories);
//            gamesGroup.addAppInfo(tetris);
        gamesGroup.addAppInfo(arithmeticGame);
        tempList.add(gamesGroup);
        //Tool
        AppGroupInfo toolsGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_tools), R.drawable.menu_icon_tool);
        toolsGroup.addAppInfo(calendar);
        toolsGroup.addAppInfo(recorder);
        toolsGroup.addAppInfo(calculator);
        toolsGroup.addAppInfo(appInfo7);
        toolsGroup.addAppInfo(stopwatch);
        toolsGroup.addAppInfo(timer);
//            toolsGroup.addAppInfo(appMotionCapture);
        tempList.add(toolsGroup);
        //Setting
        AppGroupInfo settingGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_setting), R.drawable.menu_icon_settings);
        settingGroup.addAppInfo(appInfo9);
        tempList.add(settingGroup);
        return tempList;
    }


    private ArrayList<AppGroupInfo> getUnbindStatusAppGroupList() {
        ArrayList<AppGroupInfo> tempList = new ArrayList<>();
        //Phone
        AppGroupInfo phoneGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_phone), R.drawable.menu_icon_phone);
        phoneGroup.addAppInfo(appPhone);
        tempList.add(phoneGroup);
        //WhatsApp
        AppGroupInfo whatsAppGroup = new AppGroupInfo("WhatsApp", R.drawable.app_icon_whatsapp);
        whatsAppGroup.addAppInfo(whatsApp);
        tempList.add(whatsAppGroup);
        //Message
//            AppGroupInfo messageGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_friends), R.drawable.menu_icon_message);
//            messageGroup.addAppInfo(appChat);
//            unBindStatusAppGroupList.add(messageGroup);
        //Prayer Time
//            AppGroupInfo prayerTimeGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_prayer_time), R.drawable.menu_icon_prayer);
//            prayerTimeGroup.addAppInfo(appPrayerTime);
//            unBindStatusAppGroupList.add(prayerTimeGroup);
        //Tasbih
        AppGroupInfo tasbihGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_tashbih), R.drawable.menu_icon_tasbih);
        tasbihGroup.addAppInfo(appTasbih);
        tempList.add(tasbihGroup);
        //Quran Player
        AppGroupInfo quranPlayer = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_quran_player), R.drawable.app_icon_quran_player);
        quranPlayer.addAppInfo(appInfo16);
        tempList.add(quranPlayer);
        //Religious Education
        AppGroupInfo religiousEducationGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_education), R.drawable.menu_icon_education);
        religiousEducationGroup.addAppInfo(appInfo13);
        religiousEducationGroup.addAppInfo(appInfo12);
        religiousEducationGroup.addAppInfo(appInfo14);
        religiousEducationGroup.addAppInfo(appInfo15);
        religiousEducationGroup.addAppInfo(appAlphabetLearn);
        tempList.add(religiousEducationGroup);
        //Challenge
//            AppGroupInfo challengeGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_activity), R.drawable.menu_icon_challenge);
//            challengeGroup.addAppInfo(appChallenge);
//            unBindStatusAppGroupList.add(challengeGroup);
        //Games
        AppGroupInfo gamesGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_games), R.drawable.menu_icon_games);
//            gamesGroup.addAppInfo(stories);
//            gamesGroup.addAppInfo(tetris);
        gamesGroup.addAppInfo(arithmeticGame);
        tempList.add(gamesGroup);
        //Tool
        AppGroupInfo toolsGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_tools), R.drawable.menu_icon_tool);
        toolsGroup.addAppInfo(calendar);
        toolsGroup.addAppInfo(recorder);
        toolsGroup.addAppInfo(calculator);
        toolsGroup.addAppInfo(appInfo7);
        toolsGroup.addAppInfo(stopwatch);
        toolsGroup.addAppInfo(timer);
//            toolsGroup.addAppInfo(appMotionCapture);
        tempList.add(toolsGroup);
        //Setting
        AppGroupInfo settingGroup = new AppGroupInfo(BaseApplication.getInstance().getString(R.string.menu_setting), R.drawable.menu_icon_settings);
        settingGroup.addAppInfo(appInfo9);
        tempList.add(settingGroup);
        return tempList;
    }

    public static void handleAppAction(AppInfo app, Context context) {
        if (app.cls.equals(AppEntry.Guide.CLS)) {
            Intent intent = new Intent(context, GuideActivity.class);
            intent.putExtra("is_done_finish", true);
            context.startActivity(intent);
        } else if (app.cls.equals(AppEntry.Dial.CLS)) {
            Intent intent = new Intent(context, DialPadActivity1.class);
            context.startActivity(intent);
        } else if (app.pkg.equals("com.umeox.watch.moto.ptt")) {
            //启动联系人选择界面
            Uri uri = Uri.parse("contact://view/" + "PTT_CHOOSE_CONTACT");
            Intent pttIntent = new Intent("com.wherecom.intent.action.CHOOSE_CONTACT", uri);
            pttIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(pttIntent);
        } else if (app.pkg.equals(AppEntry.Pedometer.PKG)) {
            EventBus.getDefault().post(new HorizontalScrollToPagerEvent(0));
        } else {
            ComponentName componentName = new ComponentName(app.pkg, app.cls);
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setComponent(componentName);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            context.startActivity(intent);
        }
    }
}
